package pvm.test;

///import java.nio.ByteBuffer;
import java.io.*;

import pvm.test.R;
import android.app.Activity;
import android.os.Bundle;
import android.content.res.Resources;
import android.content.Context;
import android.app.AlertDialog;


public class PvmTestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        try
        {        
	        deviceHandle = testPvmInit(0, 44100, 16, 2);
	        if(deviceHandle<100)
	        	deviceHandle = 0;
	        
	        if(deviceHandle!=0)
	        {
	        	Context context = this;
	        	Resources res = context.getResources();
	        	//InputStream	is = res.openRawResource(R.raw.test);
	        	//InputStream	is = res.openRawResource(R.raw.game01);
	        	InputStream	is = res.openRawResource(R.raw.test);
	        	int dataSize =is.available();
	        	byte[] data = new byte[dataSize]; 
	        	int readRes = is.read(data, 0, data.length);
	        	if(readRes==data.length)
	        	{
	        		pvmAudioHandle = testPvmLoadAudioFromArray(data);
	        		if(pvmAudioHandle<100)
	        			pvmAudioHandle = 0;
	        	}
	        }

	        if(pvmAudioHandle==0)
	        {
	        	AlertDialog alertDialog;
	        	alertDialog = new AlertDialog.Builder(this).create();
	        	alertDialog.setTitle("Error!");
	        	alertDialog.setMessage("Can't load audio!");
	        	alertDialog.show();
	        }
	        else
	        {
	        	currentVoiceNumber = testPvmPlayAudio(deviceHandle, pvmAudioHandle);
	        }
        }
        catch(Exception ex)
        {
        	ex.printStackTrace();
        }
    }
    
    @Override
    protected void onPause() 
    {
    	testPvmSuspendDevice(deviceHandle);
    	super.onPause();
    }

    @Override
    protected void onResume()
    {
    	super.onResume();
    	testPvmResumeDevice(deviceHandle);
    }
    
    @Override
    protected void onDestroy ()
    {
    	if(currentVoiceNumber>=0)
    	{
    		testPvmStopAudio(deviceHandle, currentVoiceNumber);
    	}
    	if(pvmAudioHandle!=0)
    		testPvmFreeAudio(pvmAudioHandle);
    	pvmAudioHandle = 0;
    	testPvmDeinit(deviceHandle);
    	super.onDestroy();
    }
    
    private int deviceHandle = 0;
    private int pvmAudioHandle = 0;
    private int currentVoiceNumber = -1;
    
    private native int testPvmInit(int nDevice, int sampleRate, int bitsPerSample, int chanelsNum);
    private native void testPvmDeinit(int device);
    private native int testPvmLoadAudioFromArray(byte[] inArray);
    private native void testPvmFreeAudio(int audioHandle);
    private native int testPvmPlayAudio(int deviceHandle, int audioHandle);
    private native void testPvmStopAudio(int deviceHandle, int voiceNum);
    private native void testPvmSuspendDevice(int deviceHandle);
    private native void testPvmResumeDevice(int deviceHandle);
    
    static 
    {
    	try
    	{
    		System.loadLibrary("pvmtest");
    	}
    	catch(Exception ex)
    	{
    		ex.printStackTrace();
    	}
    }
}